package com.oristand.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.oristand.mapper.ProductMapper;
import com.oristand.mapper.StoreMapper;
import com.oristand.mapper.StoreProductMapper;
import com.oristand.pojo.Product;
import com.oristand.pojo.Store;
import com.oristand.pojo.StoreProduct;
import com.oristand.pojo.vo.StoreProductVo;
import com.oristand.service.StoreProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 用于解决店铺和商品的关联问题
 * example:
 * 一个店铺有多个商品
 * 一个商品被多个店铺售卖
 *
 * @author shiyjian
 * @version 1.0
 * @date 2021/12/1 19:53
 */
@Service
public class StoreProductServiceImpl implements StoreProductService {

    private StoreProductMapper storeProductMapper;
    private StoreMapper storeMapper;
    private ProductMapper productMapper;

    @Autowired
    public void setStoreProductMapper(StoreProductMapper storeProductMapper) {
        this.storeProductMapper = storeProductMapper;
    }

    @Autowired
    public void setStoreMapper(StoreMapper storeMapper) {
        this.storeMapper = storeMapper;
    }

    @Autowired
    public void setProductMapper(ProductMapper productMapper) {
        this.productMapper = productMapper;
    }

    /**
     * 查询一个店铺有多少个商品信息
     *
     * @param storeId 店铺id
     * @param status  status是可选条件,1或null:查询所有商品,2:查询在售商品,3:查询下架商品
     * @return 返回StoreProductVo集合对象
     * @see StoreProductVo 一个店铺对象,对应多个商品对象
     */
    @Override
    public StoreProductVo findStoreProductList(Integer storeId, Integer status) {
        StoreProductVo storeProductVo = new StoreProductVo();
        //先判断店铺id不为空
        if (storeId != null) {
            //找出这个店铺
            Store store = storeMapper.selectById(storeId);
            storeProductVo.setStore(store);
            //准备店铺的商品
            List<Product> productList = new ArrayList<>();
            //构造查询条件,status
            QueryWrapper<StoreProduct> storeProductQueryWrapper = obtainQueryWrapper1(
                    new QueryWrapper<StoreProduct>().eq("store_id", storeId),
                    status);
            //找到这个中间表的集合
            List<StoreProduct> storeProducts = storeProductMapper.selectList(storeProductQueryWrapper);
            //遍历集合查找该店铺的商品,放在productList中
            if (Objects.nonNull(storeProducts) && !storeProducts.isEmpty()) {
                for (StoreProduct storeProduct : storeProducts) {
                    Integer productId = storeProduct.getProductId();
                    Product product = productMapper.selectById(productId);
                    productList.add(product);
                }
            }
            //整合结果,返回对象
            storeProductVo.setProductList(productList);
            return storeProductVo;
        }
        return null;
    }

    /**
     * 找出这个商店有多少商品,返回的是商品个数
     *
     * @param storeId 店铺id
     * @param status  状态码 status是可选条件,1或null:查询所有商品,2:查询在售商品,3:查询下架商品
     * @return 返回商品个数
     */
    @Override
    public int findStoreProductCount(Integer storeId, Integer status) {
        //构造查询条件,status
        QueryWrapper<StoreProduct> storeProductQueryWrapper = obtainQueryWrapper1(
                new QueryWrapper<StoreProduct>().eq("store_id", storeId),
                status);
        Long count = storeProductMapper.selectCount(storeProductQueryWrapper);
        return count.intValue();
    }

    /**
     * 通过switch-case语句来构造这个查询条件
     *
     * @param queryWrapper 方法传过来的queryWrapper
     * @param status       状态码
     * @return 返回一个查询条件对象
     */
    private QueryWrapper<StoreProduct> obtainQueryWrapper1(QueryWrapper<StoreProduct> queryWrapper, Integer status) {
        if (status != null) {
            switch (status) {
                case 1:
                    break;
                case 2:
                    queryWrapper.eq("status", 1);
                    break;
                case 3:
                    queryWrapper.eq("status", 0);
                    break;
                default:
                    break;
            }
        }
        return queryWrapper;
    }
}
